from django.contrib.auth.models import AnonymousUser
from django.shortcuts import render
# from rest_framework import serializers
# Create your views here.
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.response import Response
from rest_framework.views import APIView

# from apps.center.serializer01 import QuestionModelSerializer
from apps.question.models import Label, Question, Reply
from apps.question.serializers.labels import UserLabel
from apps.question.serializers.question import QuestionModelSerializer


class LabelListAPIView(ListAPIView):
    # 用户关注的标签信息
    permission_classes = []
    serializer_class = UserLabel

    def get_queryset(self):
        user = self.request.user
        if user.id is None:
            return Label.objects.all()
        labels = user.labels.all()
        return labels

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        if len(queryset) == 0:
            return Response([])
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)


class LabelQuestionAPIView(APIView):
    # 最新回复，未完成
    def get(self, request, id):
        return Response([])


class HostReplyAPIView(APIView):
    # 热门回复，未完成
    def get(self, request, id):
        return Response([])


class WaitQuestionAPIView(ListAPIView):
    # 等待回答的问题
    permission_classes = []
    serializer_class = QuestionModelSerializer
    # 通过label 查询 question 显示 question.replies is None 的question

    def get_queryset(self):
        label_id = self.kwargs.get('id')
        if int(label_id) == -1:
            # 所有标签
            questions = Question.objects.filter(reply=0)
            # for question in questions:
            #     if question.replies:
            #         question.pop()
            return questions
            # questions_queryset = labels.none
            # q_list = []
            # # questions_queryset.clear()
            # for label in labels:
            #     questions = label.questions.all()
            #     question_set = set()
            #     # question_dict = {}
            #     for question in questions:
            #         if question.replies:
            #             question_set.add(question)
            #     for question in questions:
            #         if question in question_set:
            #             question.delete()
            #     q_list.append(questions)
            # return q_list
        else:
            # 一个标签
            label = Label.objects.get(id=label_id)
            questions = label.questions.all()
            # question_set = set()
            # question_dict = {}
            for question in questions:
                if question.reply:
                    question.delete()
            # for question in questions:
            #     if question in question_set:
            #         question.delete()
            #     # question_dict.append(question)
            return questions
            # for question in questions:
            #     if question.replies is None:


class CreateQuestionAPIView(APIView):
    # 发布一个问题
    permission_classes = []

    def post(self, request):
        user = request.user
        data = request.data
        create_data = {}
        # create_data.append(data.pop('labels'))
        create_data['content'] = data.get('content')
        create_data['title'] = data.get('title')
        create_data['user'] = user
        question = Question.objects.create(**create_data)
        labels = data.get('labels')
        for label in labels:
            label_obj = Label.objects.get(id=label)
            label_obj.questions.add(question)
        return Response({'message': '发布成功', 'success': True})


class QuestionDetail(RetrieveAPIView):
    # 问题详情
    permission_classes = []
    parser_classes = Question

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        return Response(serializer.data)

    # def get_queryset(self):
    #     id =
    #     return


class ReplyQuestion(APIView):
    # 回答问题
    permission_classes = []

    def post(self, request):
        user = request.user
        data = request.data
        problem_id = data.get('problem')
        problem = Question.objects.get(id=problem_id)
        problem.reply += 1
        reply_obj = Reply.objects.create(
            user=user,
            problem=problem,
            type=data.get('type'),
            content=data.get('content')
        )
        if data.get('parent'):
            obj = Reply.objects.get(id=data.get('parent'))
            reply_obj.parent=obj
            reply_obj.save()
        return Response({'message': '回复成功', 'success': True})


# 用户关注的标签列表
user_labels = LabelListAPIView.as_view()
# 最新回复
new_reply = LabelQuestionAPIView.as_view()
# 热门回答
host_reply = HostReplyAPIView.as_view()
# 发布问题
create_question = CreateQuestionAPIView.as_view()
# 等待回答的问题
wait_question = WaitQuestionAPIView.as_view()
# 问题详情
question_detail = QuestionDetail.as_view()
# 回答问题
reply_question = ReplyQuestion.as_view()
